package org.fastfaild.linked_list;

/**
 * className: LinkedList
 * Description:
 * date: 2022/11/30 10:35
 *
 * @author fanpeng
 * @version 1.0
 */
public class LinkedList<E> implements List<E>{

    transient int size = 0;

    transient Node<E> first;

    transient Node<E> last;

    public LinkedList() {
    }


    /**
     * 头插法
     * @param e
     */
    void LinkFirst(E e){
       final Node<E> f = first;
       final Node<E> newNode = new Node<>(null,e ,f);
       first = newNode;
       if(f == null){
           last = newNode;
       }else {
           f.prev = newNode;
       }
       size++;
    }

    /**
     * 尾插法
     * @param e
     */
    void linkLast(E e){
        final Node<E> l = last;
        final Node<E> newNode = new Node<>(l,e,null);
        last = newNode;
        if (l == null) {
            first = newNode;
        }else {
            l.next = newNode;
        }
        size++;
    }

    /**
     * 拆链
     * @param x
     * @return
     */
    E unlink(Node<E> x) {
        final E element = x.item;
        final Node<E> next = x.next;
        final Node<E> prev = x.prev;

        if (prev == null) {
            last = prev;
        }else {
            prev.next = next;
            x.next = null;
        }

        if (next == null){
            first = next;
        }else {
            next.prev = prev;
            x.next = null;
        }
        x.item = null;
        size--;
        return element;
    }
    @Override
    public boolean add(E e) {
        linkLast(e);
        return true;
    }

    @Override
    public boolean addFirst(E e) {
        LinkFirst(e);
        return true;
    }

    @Override
    public boolean addLast(E e) {
        linkLast(e);
        return true;
    }

    @Override
    public boolean remove(Object o) {
        Node<E> x = first;
        if(o == null){
            while (x.next != null){
                if(x.item == null){
                    unlink(x);
                    return true;
                }
                x = x.next;
            }
        }else {
            while (x.next != null){
                if(o.equals(x.item)){
                    unlink(x);
                    return true;
                }
                x = x.next;
            }
        }
        return false;
    }

    @Override
    public E get(int index) {
        return node(index).item;
    }
    Node<E> node(int index){
        if (index < (size >> 1)) {
            Node<E> x = first;
            for (int i = 0; i < index; i++) {
                x = x.next;
            }
            return x;
        }else {
            Node<E> x = last;
            for (int i = (size - 1); i > index; i--) {
                x = x.prev;
            }
            return x;
        }
    }

    @Override
    public void printLinkList() {

        if (size == 0) {
            System.out.println("链表为空");
        }else {
            Node<E> temp = first;
            System.out.println("目前列表，头节点:" + first.item +"尾节点：" + last.item + " 整体：");
            while (temp != null){
                System.out.println(temp.item + ", ");
                temp = temp.next;
            }
            System.out.println();
        }

    }

    private static class Node<E> {

        E item;

        Node<E> next;

        Node<E> prev;

        public Node(Node<E> prev, E element, Node<E> next) {
            this.item = element;
            this.next = next;
            this.prev = prev;
        }
    }
}
